import django.utils.timezone as timezone
from django.db import models


class BaseModel(models.Model):
    """
    基础模型，包含创建时间和更新时间字段
    """
    is_delete = models.IntegerField("数据逻辑状态", choices=((1, "无效"), (0, "有效")), default=0)
    pt_flag = models.IntegerField("压测标志", choices=((1, "压测数据"), (0, "正常数据")), default=0)
    create_time = models.DateTimeField("创建时间", default=timezone.now)
    update_time = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    class Meta:
        abstract = True  # 表示这是一个抽象类，不会生成对应的表
        ordering = ['-create_time']


class BaseSortModel(BaseModel):
    sort_num = models.SmallIntegerField("排序号", default=0)
    CHOICES_STATUS = ((0, "关闭"), (1, "开启"))
    is_active = models.SmallIntegerField("状态", choices=CHOICES_STATUS, default=1)

    class Meta:
        abstract = True
        ordering = ["-create_time"]


class SMSProvider(BaseSortModel):
    sms_name = models.CharField('模板标题', max_length=20, help_text="模板标题")
    sms_key = models.CharField('开发者账号', max_length=128, default="待配置", help_text="API Key")
    sms_secret = models.CharField('开发者密码', max_length=128, default="待配置", help_text="API Secret")
    sms_description = models.TextField('描述说明', blank=True, default="", help_text="模板的详细用途说明")

    class Meta:
        db_table = 'sms_provider'
        verbose_name = '短信供应商'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']  # 默认按名称排序

    def __str__(self):
        return f"{self.sms_name} (wx_app_id: {self.sms_key} wx_app_secret: {self.sms_secret}))"


class SMSTemplate(BaseSortModel):
    name = models.CharField('模板标题', max_length=128, unique=True, help_text="模板标题（唯一标识）")
    sms_provider = models.ForeignKey(SMSProvider, on_delete=models.PROTECT, db_constraint=False)
    sign = models.CharField('模板签名', max_length=128, help_text="模板签名")
    template_id = models.CharField('模板CODE', max_length=128, help_text="模板CODE")
    template_data = models.TextField('模板变量', blank=True, default="", help_text="JSON格式的模板变量数据")
    description = models.TextField('描述说明', blank=True, default="", help_text="模板的详细用途说明")

    class Meta:
        db_table = 'sms_template'
        verbose_name = '短信模板消息'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']  # 默认按名称排序

    def __str__(self):
        return f"{self.name} (ID: {self.template_id})"


class SMSLog(BaseModel):
    phone = models.CharField('手机号', max_length=20, help_text="发送的手机号")
    sign = models.CharField('模板签名', max_length=64, help_text="模板签名")
    template_id = models.CharField('模板CODE', max_length=128, help_text="模板CODE")
    send_time = models.DateTimeField("发送时间", default=timezone.now)
    content = models.CharField('短信内容', max_length=256, help_text="短信内容")
    is_send = models.IntegerField("发送状态", choices=((0, "失败"), (1, "成功")), default=1)

    class Meta:
        db_table = 'sms_log'
        verbose_name = '短信发送记录'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']  # 默认按名称排序

    def __str__(self):
        return f"{self.phone} (ID: {self.content})"
